package org.outlook.commons.ui.components.table;

import javax.swing.Icon;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

public class TextWithIconCellRenderer extends DefaultTableCellRenderer {

	private static final long serialVersionUID = 1L;

	protected void setValue(Object value) {
		if (value instanceof DataWithIcon) {
			if (value != null) {
				DataWithIcon d = (DataWithIcon) value;
				Object dataValue = d.getData();

				setText(dataValue == null ? "" : dataValue.toString());
				setIcon(d.getIcon());
				setHorizontalTextPosition(SwingConstants.RIGHT);
				setVerticalTextPosition(SwingConstants.CENTER);
				setHorizontalAlignment(SwingConstants.LEFT);
				setVerticalAlignment(SwingConstants.CENTER);
			} else {
				setText("");
				setIcon(null);
			}
		} else {
			super.setValue(value);
		}
	}

	class DataWithIcon {
		  public DataWithIcon(Object data, Icon icon) {
		    this.data = data;
		    this.icon = icon;
		  }

		  public Icon getIcon() {
		    return icon;
		  }

		  public Object getData() {
		    return data;
		  }

		  public String toString() {
		    return data.toString();
		  }

		  protected Icon icon;

		  protected Object data;
		}
}